<?php

require_once dirname(__FILE__) . '/WideFileUser.class.php';

/**
 * Description of Session
 *
 * @author Henrik Tudborg
 */
class WideSession
{

    private $user;

//private $userlist; // dont really think this is needed anymore?
//private $usergroups;

    public function __construct()
    {
        //start session
        $this->start();

        // handle logout
        if (isset($_GET['logout']))
        {
            $this->destroy(); // destroy session
        }


        // handle user auth
        if (!empty($_SESSION['WideUserObject']) && empty($_POST['usr'])) // If the user is logged in
        {
            $this->user = $_SESSION['WideUserObject'] = new WideFileUser($_SESSION['WideUserObject']->getCurrentUser(), null, true);
        }
        else
        {
            //login required
            if (!empty($_POST['usr']) && !empty($_POST['pwd']))
            {
                $usr = new WideFileUser($_POST['usr'], $_POST['pwd']);
                if ($usr->validated)
                {
                    $this->user = $_SESSION['WideUserObject'] = $usr;
                }
                else
                {
                    $this->user = $_SESSION['WideUserObject']= new WideFileUser("guest", null, true);
                }
            } else {
                $this->user = $_SESSION['WideUserObject'] = new WideFileUser("guest", null, true);
            }
        }
    }
    // constructor finished
    

    public function destroy($redirect = true)
    {
        session_destroy();
        if ($redirect)
            header("Location:.");
    }

    private function start()
    {
        session_name("wideCMS");
        session_start();

        if (session_name() != "wideCMS")
        {
            session_destroy();
            header("Location:.");
        }
    }

    public function loggedin()
    {
        if (!empty($this->user))
            return true;
        return false;
    }

    public function user()
    {
        return $this->user;
    }

}

?>
